package com.cloudsigns.factorymanager2.project.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by tony on 2018/3/21.
 */

public class AddMaintenanceOrderModel implements IAddMaintenaceOrderModel {

    @Override
    public void addEngineeringMaintenanceOrder(final CommonEnum.MaintenanceCategory category, final String content, final String engineeringName, final int lat, final int lon, final String projectName, final String questionDesc, final OnAddMaintenanceOrderListener onAddMaintenanceOrderListener) {
        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.ADD_MAINTENANCE_ORDER_URL);

                HttpResponse response;
                try {

                    JSONObject jsonObject = new JSONObject();

                    if (null != category) {
                        if (category == CommonEnum.MaintenanceCategory.ELECTRICAL_PROBLEM) {
                            jsonObject.put("category", "ELECTRICAL_PROBLEM");
                        } else if (category == CommonEnum.MaintenanceCategory.QUALITY_ISSUES) {
                            jsonObject.put("category", "QUALITY_ISSUES");
                        }
                    }
                    jsonObject.put("content", content);
                    jsonObject.put("engineeringName", engineeringName);
                    jsonObject.put("engineeringName", engineeringName);
                    jsonObject.put("lat", lat);
                    jsonObject.put("lon", lon);
                    jsonObject.put("projectName", projectName);
                    jsonObject.put("questionDesc", questionDesc);


                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);

                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());

                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), HTTP.UTF_8).trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onAddMaintenanceOrderListener) {

                        if (code.equals("0")) {

                            onAddMaintenanceOrderListener.success(json.getString("id"));
                        } else {

                            onAddMaintenanceOrderListener.failed(code + "");


                        }

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnAddMaintenanceOrderListener {
        void success(String projectId);

        void failed(String desc);
    }
}
